﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TeamBuy.AccessWebUI
{
    public partial class Reg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            TeamBuy.AccessService.TeamBuyUserService userReg = new AccessService.TeamBuyUserService();
            int isUserReg = userReg.CreateUser(txtEmail.Text, txtPassword.Text, txtUserName.Text, Main_ComeSoonCities1.CityID);
            if (isUserReg == 1)
            {
                int loggedInUser;
                DataTable dtRegUser = userReg.GetUserByEmailAndPwd(txtEmail.Text, txtPassword.Text);
                loggedInUser = Convert.ToInt32(dtRegUser.Rows[0]["UserID"].ToString());
                if (loggedInUser >0)
                {
                    if (TeamBuySession.GetInviteUserID() > 0)
                    {
                        TeamBuy.AccessService.TeamBuyInviteService createInvite = new AccessService.TeamBuyInviteService();
                        createInvite.CreateInvite(loggedInUser, TeamBuySession.GetInviteUserID());
                    }
                    if (AccessService.TeamBuySystemInfo.IsNeedEmailActive == true)
                    {
                        string activeLink = AccessService.TeamBuySystemInfo.SiteDomain + "/ActiveUser.aspx?ID=" + Common.PublicFunction.DESEncrypt(dtRegUser.Rows[0]["UserID"].ToString()) + "&Date=" + Common.PublicFunction.DESEncrypt(dtRegUser.Rows[0]["DateCreated"].ToString()) + "&Mail=" + Common.PublicFunction.DESEncrypt(dtRegUser.Rows[0]["UserEmail"].ToString());

                        if (Common.PublicFunction.SendMailToUser(AccessService.TeamBuySystemInfo.ActiveEmailAddress, dtRegUser.Rows[0]["UserEmail"].ToString(), "感谢注册" + AccessService.TeamBuySystemInfo.SiteName + "，请验证Email", "Hi " + dtRegUser.Rows[0]["UserName"].ToString() + ",<br /><br />感谢您注册" + AccessService.TeamBuySystemInfo.SiteName + "，请点击下面的链接验证您的Email：<br /><br /><a href='http://" + activeLink + "' target='_blank'>点击激活帐户</a><br /><br /><br />" + AccessService.TeamBuySystemInfo.SiteName, AccessService.TeamBuySystemInfo.ActiveEmailSMTP, Convert.ToInt32(AccessService.TeamBuySystemInfo.ActiveEmailPort), AccessService.TeamBuySystemInfo.ActiveEmailPassword) == true)
                        {
                            Response.Redirect("~/Reged.aspx?RegedUserID=" + Common.PublicFunction.DESEncrypt(loggedInUser.ToString()));
                        }
                        else
                        {
                            Main_PublicSystemMessage1.Message = "注册成功，但激活邮件未发送成功，您可<a href='/Login.aspx" + HttpUtility.UrlEncode(Request.QueryString["RURL"]) + "'>登录</a>并选择重新发送激活链接";
                            Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                            Main_PublicSystemMessage1.MessageVisible = true;
                        }
                    }
                    else
                    {
                        userReg.ActiveUser(loggedInUser);

                        if (TeamBuySession.CreateUserSession(txtEmail.Text, txtPassword.Text, false) > 0)
                        {
                            if (Request.QueryString["RURL"] == null)
                            {
                                Response.Redirect("~/Default.aspx");
                            }
                            else
                            {
                                Response.Redirect(HttpUtility.UrlDecode(Convert.ToString(Request.QueryString["RURL"])));
                            }
                        }
                        else
                        {
                            Response.Redirect("~/Login.aspx");
                        }
                    }
                }

            }
            else if (isUserReg == 2)
            {
                Main_PublicSystemMessage1.Message = "此邮箱已注册，可直接<a href='/Login.aspx" + HttpUtility.UrlEncode(Request.QueryString["RURL"]) + "'>登录</a>";
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                Main_PublicSystemMessage1.MessageVisible = true;
            }
            else if (isUserReg == 0)
            {
                Main_PublicSystemMessage1.Message = "注册发生错误，请重试";
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                Main_PublicSystemMessage1.MessageVisible = true;
            }
        }

        protected void lbtnSubscribeOther_Click(object sender, EventArgs e)
        {
            Main_ComeSoonCities1.Enable = false;
            txtCityName.Visible = true;
        }
    }
}